// pages/groundPush/groundPush_details/groundPush_details.js
const common = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    emailStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        id: options.id
      })
    }
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  editEmail: function () {
    this.setData({
      emailStatus: !this.data.emailStatus
    })
  },
  materialEmail:function(e){
    let that = this;
    let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    let email = e.detail.Email
    if (!reg.test(email)){
      wx.showToast({
        title: '请填写正确的邮箱！',
        icon: 'none',
        duration: 1000
      })
    } else if (reg.test(email)){
      let data = {
        fodder_id: that.data.materialInfo.id,
        email: e.detail.Email
      }
      common.post('User/send_email', data).then(function (res) {
        if(res.code === 204){
          wx.showToast({
            title: res.error,
            icon: 'none',
            duration: 1000
          })
        }else{
          wx.showToast({
            title: "领取成功！",
            icon: 'none',
            duration: 1000,
            success: function(){
              wx.navigateTo({
                url: '/pages/groundPush/groundPsuh_list/groundPsuh_list',
              })
            }
          })
          that.setData({
            emailStatus: false
          })
        }
      })
    }
  },
  goBack: function () {
    if (getCurrentPages().length == 1) {
      wx.switchTab({
        url: '/pages/index/index'
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  getData: function () {
    let that = this;
    let data = {
      id: that.data.id,
      type: 5
    }
    common.post('Fodder/fodder_list', data).then(function (res) {
      if (res.code === 204) {

      } else {
        that.setData({
          materialInfo: res[0]
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    common.refresh()
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})